package com.jxmcloud.business.cut.service.impl.app.activity;

import com.jxmcloud.business.cut.constants.CutErrorMsg;
import com.jxmcloud.business.cut.dto.client.GetRecentOrderUserRankDto;
import com.jxmcloud.business.cut.dtoCommon.UserOrderRankDto;
import com.jxmcloud.business.cut.service.database.IUserLoginInfoDBService;
import com.jxmcloud.business.cut.service.user.IUserLoginInfoService;
import com.jxmcloud.business.cut.vo.client.GetRecentOrderUserRankVo;
import com.jxmcloud.business.cut.vo.user.RankBuyOrderVo;
import com.xlcloud.platform.api.exception.BusinessException;
import com.xlcloud.platform.util.PublicUtil;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import com.jxmcloud.business.cut.vo.user.PlayerInfoVo;
import com.jxmcloud.business.cut.service.app.activity.IShopBuyService;
import com.jxmcloud.business.cut.vo.client.GetRecentBuyCountVo;
import com.jxmcloud.business.cut.dto.client.GetRecentBuyCountDto;

import javax.annotation.Resource;
import java.util.List;


@Service
public class ShopBuyServiceImpl implements IShopBuyService {
    @Resource
    private IUserLoginInfoDBService userLoginInfoService;


    // 获取最近购买商品数量
    @Override
    public GetRecentBuyCountVo getRecentBuyCount(GetRecentBuyCountDto dto, PlayerInfoVo player) {
        String error = CutErrorMsg.ERROR_FALSE;
        GetRecentBuyCountVo result = new GetRecentBuyCountVo();
        do {
            Integer count = player.getUserOrderListVo().getTotalBuyNum();
            result.setCount(count);
            error = CutErrorMsg.ERROR_TRUE;
        } while (false);
        if (!error.equals(CutErrorMsg.ERROR_TRUE)) {
            // 抛出异常
            throw new BusinessException(error);
        }
        return result;
    }

    @Override
    public GetRecentOrderUserRankVo getRecentOrderUserRank(GetRecentOrderUserRankDto dto, PlayerInfoVo player) {
        String error = CutErrorMsg.ERROR_FALSE;
        GetRecentOrderUserRankVo result = new GetRecentOrderUserRankVo();
        do {
            List<RankBuyOrderVo> rankBuyOrderVos = userLoginInfoService.selectRankBuyOrder();
            if (PublicUtil.isEmpty(rankBuyOrderVos)) {
                result.setList(Lists.newArrayList());
                break;
            }
            result.setList(PublicUtil.copyList(rankBuyOrderVos, UserOrderRankDto.class));

            error = CutErrorMsg.ERROR_TRUE;
        } while (false);
        if (!error.equals(CutErrorMsg.ERROR_TRUE)) {
            // 抛出异常
            throw new BusinessException(error);
        }
        return result;
    }
}
